import { defineConfig, loadEnv } from 'vite'
import { fileURLToPath } from 'url';
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig(({ command, mode }) => {
  const { VITE_ENV_BASE_API, VITE_ENV_BASE_URL } = loadEnv(mode, process.cwd());
  return {
    plugins: [vue(), AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
      // 排除有命名冲突的组件，手动导入
      exclude: [
        /LoginAndName\.vue$/,
        /Language\.vue$/,
        /Search\.vue$/,
        /Setup\.vue$/,
        /UserInfo\.vue$/
      ],
      // 只扫描特定目录
      dirs: ['src/components'],
      // 避免深度扫描导致冲突
      deep: false
    }),],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    // 优化依赖预构建
    optimizeDeps: {
      include: [
        'vue',
        'vue-router',
        'element-plus',
        'element-plus/es/components/button/style/css',
        'element-plus/es/components/menu/style/css',
        'element-plus/es/components/table/style/css',
        'element-plus/es/components/pagination/style/css',
        '@element-plus/icons-vue',
        'pinia',
        '@vueuse/core',
        'idb'
      ],
      exclude: ['@yeying-community/yeying-client-ts']
    },
    css: {
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler', // sass BANBEN 1.60.0 以上版本才支持
          additionalData: '@use "@/assets/css/whole.scss";'
        }
      }
    },
    build: {
      outDir: VITE_ENV_BASE_URL,
      sourcemap: command === 'serve',
      rollupOptions: {
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
          // 手动分包优化
          manualChunks: {
            'vue-vendor': ['vue', 'vue-router', 'pinia'],
            'element-plus': ['element-plus'],
            'utils': ['@vueuse/core', 'idb']
          }
        },
      },
      // 构建优化
      minify: 'esbuild',
      target: 'esnext',
      reportCompressedSize: false,
    },
    // esbuild: {
    //   drop: command === 'build' ? ["console"] : [],
    // },
    // base: command === 'serve' ? '' : "/" + VITE_ENV_BASE_URL + "/",
    base: command === 'serve' ? '' : "./",
    server: {
      // 开发服务器优化
      hmr: {
        overlay: false
      },
      // 预热常用文件
      warmup: {
        clientFiles: [
          './src/components/layout/AppLayout.vue',
          './src/components/shared/Layout/TeacherMainLayout.vue',
          './src/components/shared/Layout/StudentMainLayout.vue',
          './src/components/shared/Layout/SideNavigation.vue',
          './src/views/teacher/Dashboard/TeacherDashboard.vue',
          './src/views/student/Dashboard/StudentDashboard.vue',
          './src/views/DevelopmentIndex.vue'
        ]
      },
      proxy: {
        // 代理规则示例1：简单路径代理  // agent 用户管理
        '/registry': {
          target: VITE_ENV_BASE_API, // 后端服务器地址
          changeOrigin: true, // 修改请求头中的 Origin 为目标地址
        },
        '/whoami': {
          target: VITE_ENV_BASE_API, // 后端服务器地址
          changeOrigin: true, // 修改请求头中的 Origin 为目标地址
        },
      },
    },
  }
})
